using System;
using System.Collections.Generic;
using System.Text;
using Barrage.Global;
using SlimDX.XInput;
using SlimDX;

namespace Barrage.Elements.Cameras
{
    /// <summary>
    /// An OrbitCamera instance that can be moved by using the mouse or the gamepad
    /// </summary>
    public class BasicOrbitCamera:OrbitCamera
    {
        int playerIndex = (int)UserIndex.Any;

        private float Radius { get { return radius; } set { radius = Math.Max(float.Epsilon, value); } }
        
        /// <summary>
        /// Inits the camera's matrices. Anyone will be able to move this camera
        /// </summary>
        /// <param name="targetMotion">Motion matrix around which the camera will orbit</param>
        public BasicOrbitCamera(Motion targetMotion)
            : base()
        {
            Target = targetMotion;
        }

        /// <summary>
        /// Inits the camera's matrices and sets the player index which will be able to move this camera
        /// </summary>
        /// <param name="targetMotion">Motion matrix around which the camera will orbit</param>
        /// <param name="playerIndex">Player's index which will be able to move this camera</param>
        public BasicOrbitCamera(Motion targetMotion,int playerIndex):this(targetMotion)
        {
            this.playerIndex = playerIndex;
        }

        /// <summary>
        /// \internal Updates camera position and orientation
        /// </summary>
        protected override void DoUpdate()
        {
            heading += Mouse.Instance.Shift.X * 0.002f;
            pitch += Mouse.Instance.Shift.Y * 0.002f;

            if (playerIndex != (int)UserIndex.Any)
            {
                Barrage.Global.Gamepad gamepad = Barrage.Global.Gamepad.Instance((UserIndex)playerIndex);
                Vector2 rStickPos = gamepad.StickPosition(Stick.RightStick);
                Vector2 lStickPos = gamepad.StickPosition(Stick.LeftStick);
                heading += rStickPos.X * 0.02f;
                pitch -= rStickPos.Y * 0.02f;
            }
            
            Radius *= 1-Mouse.Instance.WheelVariation/20;

            base.DoUpdate();
        }
    }
}
